package com.lry.basic.juc;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

public class SoftRefCache<K,V> {

    private Map<K, SoftReference<V>> cache;

    public SoftRefCache(){
        cache = new HashMap<>();
    }

    public void put(K k,V v){
        cache.put(k,new SoftReference<>(v));
    }

    public V get(K k){
        SoftReference<V> softReference = cache.get(k);
        if(null==softReference)
            return null;
        return softReference.get();
    }

    public void remove(K k){
        SoftReference<V> softReference = cache.get(k);
        if(null==softReference)
            return;
        softReference.clear();
        cache.remove(k);
    }

    public static void main(String[] args) {
        SoftRefCache<String,Integer> cache = new SoftRefCache<>();
        cache.put("xm",20);
        cache.remove("xm");
        System.out.println(cache.get("xm"));

    }
}
